<?php
// 连接到Redis服务器  
// 实例化了类
@$redis = new Redis(); 
// 链接redis  ip  // 端口
$redis->connect('127.0.0.1', 6379);

$userName = $redis->get("name") ?: null;
$number  = $redis->get("number") ?: null;
// 检查是否有POST请求  
if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $key = htmlspecialchars($_POST['key']);
    $value = htmlspecialchars($_POST['value']);

    // 根据操作类型执行不同的Redis命令  
    switch ($_POST['operation']) {
        case 'set':
            $redis->set($key, $value);
            echo "SET operation successful.";
            break;
        case 'get':
            $result = $redis->get($key);
            if ($result === false) {
                echo "Key not found.";
            } else {
                echo "GET result: $result";
            }
            break;
        case 'incr':
            $result = $redis->incr($key);
            if ($result === false) {
                echo "Key not found or not an integer.";
            } else {
                echo "INCR result: $result";
            }
            break;
        default:
            echo "Invalid operation.";
    }
}
?>

<!DOCTYPE html>
<html>

<head>
    <title>Redis Operations</title>
</head>

<body>
    <h1>Redis number ::: <? echo $number;?></h1>
    <h1>Redis Operations <?php echo $userName;?></h1>
    <form method="post" action="">
        <label for="key">Key:</label>
        <input type="text" id="key" name="key" required><br><br>
        <label for="value">Value (for SET):</label>
        <input type="text" id="value" name="value"><br><br>
        <input type="radio" id="operation_set" name="operation" value="set">
        <label for="operation_set">SET</label><br>
        <input type="radio" id="operation_get" name="operation" value="get">
        <label for="operation_get">GET</label><br>
        <input type="radio" id="operation_incr" name="operation" value="incr">
        <label for="operation_incr">INCR</label><br><br>
        <input type="submit" value="Submit">
    </form>
</body>

</html>